#!/usr/bin/perl

use strict;
 
use DBI;
use CGI;
# use TTrakerSite;
use TTraker;

my $form_ref = &parse_input;
print_header();
my $dbh;
& initialize_dbi;

my %FORM_DATA = ();
%FORM_DATA = %$form_ref; 
my $num = $FORM_DATA{'supNum'};
my $name = $FORM_DATA{'supName'};
my $job_id = $FORM_DATA{'supJob'};
my $loc = $FORM_DATA{'supLoc'};
my $start = $FORM_DATA{'start'};
my $end = $FORM_DATA{'end'};
my $weekNum = $FORM_DATA{'weekNum'};
my $payNum = $FORM_DATA{'payNum'};

my $now = get_local_time($loc);

my %updates= ();
foreach $key (keys %FORM_DATA) {
  if ($key =~ /^payroll_data_/ ) {
 	 if ( $FORM_DATA{$key} eq "") { 
 	   $FORM_DATA{$key} = 0.0;
 	 }  
    $id_str = $key;
    $id_str =~ s/payroll_data_//;
    ($pay_cat,$emp_num) = split /_/, $id_str;
    $hrs = $FORM_DATA{$key};
    if ( not exists $updates{$emp_num} ) {		
	    $updates{$emp_num}=1;
	 }
  } else {   
#   print "KEY: $key - $FORM_DATA{$key}<br/>";   
  } 
}
my @Kes = keys %updates;
my @kes = sort @Kes;
foreach $key (@kes) {
   my $reg = $FORM_DATA{"payroll_data_reg_$key"};
   my $ot1 = $FORM_DATA{"payroll_data_ot1_$key"};
   my $ot2 = $FORM_DATA{"payroll_data_ot2_$key"};
   my $vac = $FORM_DATA{"payroll_data_vac_$key"};
   my $sta = $FORM_DATA{"payroll_data_sta_$key"};
   my $sic = $FORM_DATA{"payroll_data_sic_$key"};
   my $brv = $FORM_DATA{"payroll_data_brv_$key"};
   my $abs = $FORM_DATA{"payroll_data_abs_$key"};
   my $loa = $FORM_DATA{"payroll_data_loa_$key"};
   my $hrs = $reg + $ot1 + $ot2 + $vac + $sta + $sic + $brv;
#	print "Updating $key $weekNum VALUES: $key, $hrs, $reg, $ot1, $ot2, $vac, $sta, $sic, $brv, $abs, $loa, $start, $end, $weekNum, $payNum<br>";
	update_weekly_payroll_employee_hours ( $key, $hrs, $reg, $ot1, $ot2, $vac, $sta, $sic, $brv, $abs, $loa, $start, $end, $weekNum, $payNum, $now);
}     

trak_04_emp_log_in();

db_disconnect();

##################### SUBROUTINES FOLLOW  #####################


sub trak_04_emp_log_in
{
print <<ENDHEADER;

<html>
<head>
<link rel="stylesheet" type="text/css" href="${css_ref}trak.css" />
<script type="text/javascript" src="${js_ref}trak.js"></script>
<title>trak_01_emp_num.html NO NUM</title>
</head>
<body onload="startTime()">
<h2><center>${banner}</center></h2>
<hr/>
<div id="clock"></div>
<div id="box1">
   &nbsp&nbsp&nbsp&nbsp&nbsp Employee ID: <br/><br/>
</div> 
<form id="box2" autocomplete="off" name="empID" action="${cgi_ref}trak_01_emp_log_in.pl" method="post">
   <input type="text" name=empID />&nbsp&nbsp
   <input type="submit" value="Log Into system">
</form>

</body>
</html>

ENDHEADER
}

